package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.api.vo.GetUserAddress;
import com.haierCamera.customapplication.databinding.HzklActivityRepairBinding;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

/**
 * Created by AdministratorZJ
 * on 2020/3/24.
 */
public class HZKLRepairActivity extends BaseActivity {
    private HzklActivityRepairBinding binding;
    @Inject
    UserRepo repo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_repair);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        String deviceId = getIntent().getStringExtra("deviceId");
        binding.repairNum.setText(deviceId);
        binding.repairBtn.setOnClickListener(view -> {
            String repairNum = binding.repairNum.getText().toString();
            String content = binding.repairContent.getText().toString();
            String prople = binding.repairPeople.getText().toString();
            String phone = binding.repairPhone.getText().toString();
            String address = binding.repairAddress.getText().toString();
            if (TextUtils.isEmpty(repairNum)) {
                Toast.makeText(HZKLRepairActivity.this, "设备码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(content)) {
                Toast.makeText(HZKLRepairActivity.this, "请输入报修原因", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(prople)) {
                Toast.makeText(HZKLRepairActivity.this, "请输入联系人", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(HZKLRepairActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(address)) {
                Toast.makeText(HZKLRepairActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                return;
            }
            setRepairUser(repairNum, content, prople, phone, address);
        });
        getAddress();
    }

    private void setRepairUser(String repairNum, String content, String prople, String phone, String address) {
        repo.setRepair(repairNum, content, address, prople, phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    Toast.makeText(this, "报修成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                default:
                    break;
            }
        });
    }

    private void getAddress() {
        repo.getUserAddress().observe(this, getUserAddressResource -> {
            switch (getUserAddressResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, getUserAddressResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(getUserAddressResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    GetUserAddress data = getUserAddressResource.data;
                    binding.setSetAddress(data);
                    break;
                default:
                    break;
            }
        });
    }
}
